import datetime
import pandas as pd
import numpy as np
from jili.core import load,save
from jili.tool.convert import str2datetime
from jili.tool.state import config
from jili.data.db import getdb_client
import os
url_stock_bydate=r"G:\data\stock\bydate"
url_stock_byobj=r"G:\data\stock\byobj"
url_index_byobj=r"G:\data\index\byobj"
url_right=r"F:\qjs\data\right_factor.pkl"
url_stock=r"G:\data\stock"
rr=load(url_right)
def setdatapath(path):
    global url_stock_bydate,url_stock_byobj,url_index_byobj,url_right,rr,url_stock
    url_stock=os.path.abspath(os.path.join(path,"stock"))
    url_stock_bydate=os.path.abspath(os.path.join(path,"stock","bydate"))
    url_stock_byobj=os.path.abspath(os.path.join(path,"stock","byobj"))
    url_index_byobj=os.path.abspath(os.path.join(path,"index","byobj"))
    url_right = os.path.abspath(os.path.join(path, "other","right_factor.pkl"))
    rr = load(url_right)
startdate0=datetime.datetime(1990,1,1)
objs_len=4380
enddate0=datetime.datetime.now()
alldays=(enddate0-startdate0).days
freq={
    "k1d":"%Y",
    "k1M":"%Y",
    "k1w":"%Y",
    "k5m":"%Y%m%d",
    "k15m":"%Y%m%d",
    "k30m":"%Y%m",
    "k60m":"%Y%m",
}
freq0={
    "k1d":"%Y%m",
    "k1M":"%Y",
    "k1w":"%Y",
    "k5m":"%Y%m%d",
    "k15m":"%Y%m%d",
    "k30m":"%Y%m",
    "k60m":"%Y%m",
}
def iter_bardict_reversed(start=None,end=None,url=r"G:\data\stock",ktype="k1d"):
    url_base=os.path.join(url,"hfq")
    if start is None:
        start=datetime.datetime.now()
    startdatetime=str2datetime(start)
    starts = getdate(startdatetime, ktype, 0)
    if end is None:
        l = os.listdir(os.path.join(url_base, ktype, "bymonth"))
        for obji in l.sort(reverse=True):
            kdl = obji.split('_')[0]
            if kdl <= starts[0]:
                t = load(os.path.join(url_base, ktype, "bymonth", obji))
                keys = list(t.keys())
                for date in keys.sort(reverse=True):
                    if date <= startdatetime:
                        yield date, t[date]
    else:
        enddatetime = str2datetime(end)
        ends = getdate(enddatetime,ktype,0)
        l=os.listdir(os.path.join(url_base, ktype, "bymonth"))
        for obji in l.sort(reverse=True):
            kdl=obji.split('_')[0]
            if kdl <= starts[0] and kdl >= ends[0]:
                t=load(os.path.join(url_base, ktype,"bymonth", obji))
                keys=list(t.keys())
                for date in keys.sort(reverse=True):
                    if date<=startdatetime and date>=enddatetime:
                        yield date,t[date]
def get_k1d_maxdate(url=r"G:\data\stock",ktype="k1d"):
    url_base = os.path.join(url, "hfq")
    l=max(os.listdir(os.path.join(url_base, ktype, "bymonth")))
    a=load(os.path.join(url_base, ktype,"bymonth", l))
    return max(list(a.keys()))
def get_k1d_mindate(url=r"G:\data\stock",ktype="k1d"):
    url_base = os.path.join(url, "hfq")
    l=min(os.listdir(os.path.join(url_base, ktype, "bymonth")))
    a=load(os.path.join(url_base, ktype,"bymonth", l))
    return min(list(a.keys()))
def iter_bardict(start,end=None,url=r"G:\data\stock",ktype="k1d",objs=[]):
    url_base=os.path.join(url,"hfq")
    startdatetime=str2datetime(start)
    if end is None:
        end=datetime.datetime.now()
    enddatetime = str2datetime(end)
    starts = getdate(startdatetime,ktype,0)
    ends = getdate(enddatetime,ktype,0)
    if objs:
        for obji in os.listdir(os.path.join(url_base, ktype,"bymonth")):
            kdl=obji.split('_')[0]
            if kdl >= starts[0] and kdl <= ends[0]:
                t=load(os.path.join(url_base, ktype,"bymonth", obji))
                l=list(t.keys())
                l.sort()
                for date in l:
                    data0 = t[date]
                    data={}
                    for obj,v in data0.items():
                        data[obj]=v
                    if date>=startdatetime and date<=enddatetime:
                        yield date,data
    else:
        for obji in os.listdir(os.path.join(url_base, ktype,"bymonth")):
            kdl=obji.split('_')[0]
            if kdl >= starts[0] and kdl <= ends[0]:
                t=load(os.path.join(url_base, ktype,"bymonth", obji))
                l=list(t.keys())
                l.sort()
                for date in l:
                    data = t[date]
                    if date>=startdatetime and date<=enddatetime:
                        yield date,data
def getdate(t,ktype,flag=1):
    if t is None:
        t=datetime.datetime.now()
    if flag==0:
        f=freq0[ktype]
    else:
        f = freq[ktype]
    if isinstance(t,datetime.datetime):
        return (t.strftime(f),t)
    else:
        if len(t)==4:
            tt=datetime.datetime.strptime(t,"%Y")
        elif len(t)==6:
            tt = datetime.datetime.strptime(t, "%Y%m")
        elif len(t.split("-"))==3:
            tt = datetime.datetime.strptime(t, "%Y-%m-%d")
        else:
            tt = datetime.datetime.strptime(t, "%Y%m%d")
        return (tt.strftime(f), tt)
def getstock_kdata(objs,ktype="k1d",startdate="1990",enddate=None,adjustflag="hfq",feild=None,dtype="df"):
    global url_stock, url_right, rr
    if adjustflag=="hfq":
        url_stock0=os.path.abspath(os.path.join(url_stock,"hfq"))
    else:
        url_stock0=url_stock
    """
    Args:
        objs: str,一个代码或是  all or list:[] 交易所代码
        ktype: str :k1d,k1dw,k1M,K5m,k15m,k30m,k60m
        startdate:str :YYYYMMDD,None:全部
        enddate:str :YYYYMMDD,None:全部
        adjustflag:str :qfq,hfq,bfq
        feild:list or none
        dtype:str:df or other

    Returns:
        objs str:单重索引的数据框
        objs list :多重索引数据框
    """
    starts=getdate(startdate,ktype)
    ends=getdate(enddate,ktype)
    k=[]
    if isinstance(objs, str):
        if objs in ["all",""]:
            for kdl in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear"))):
                if kdl >= starts[0] and kdl <= ends[0]:
                    for obji in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl))):
                        k.extend(load(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl,obji))))
        else:
            for kdl in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear"))):
                if kdl >= starts[0] and kdl <= ends[0]:
                    for obji in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl))):
                        if obji.split("_")[0]==objs:
                            k.extend(load(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl, obji))))

    else:
        for kdl in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear"))):
            if kdl >= starts[0] and kdl <= ends[0]:
                for obji in os.listdir(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl))):
                    if obji.split("_")[0] in objs:
                        k.extend(load(os.path.abspath(os.path.join(url_stock0, ktype,"byobjyear", kdl, obji))))
    #转dataframe 并修剪内容
    if dtype=="df":
        if k:
            rst = pd.DataFrame(k)
            print(len(k))
            rst.set_index(["timekey", "obj"], inplace=True,drop=False)
            rst.sort_index(inplace=True)
            k1 = rst.index.get_level_values("timekey")
            if feild:
                if enddate is None:
                    rst = rst.loc[(rst["timekey"] >= starts[1]), feild]
                else:
                    rst = rst.loc[(rst["timekey"] >= starts[1]) & (rst["timekey"] <= ends[1]), feild]
            else:
                if enddate   is None:
                    rst = rst.loc[(rst["timekey"] >= starts[1])]
                else:
                    rst = rst.loc[(rst["timekey"] >= starts[1]) & (rst["timekey"] <= ends[1])]
            return rst
        else:
            return pd.DataFrame()
    else:
        return k




right_feild=["open","close","high","low"]
def holdfield(k,feild):
    d=[]
    for i in k.columns:
        if i not in feild:
            d.append(i)
    if d:
        k.drop(d,axis=1)
def bingji(a,b):
    rst=[]
    for i in a:
        if i in b:
            rst.append(i)
    return rst
def df_round(k,feild,n):
    f={}
    for i in feild:
        f[i]=n
    return k.round(f)
def adjust(objs,k,flag,startdate=None,enddate=None):
    global url_stock_bydate, url_stock_byobj, url_index_byobj, url_right, rr
    feild_r=bingji(right_feild,k.columns)
    if flag=="qfq":
        flags="qfq_factor_current"
    else:
        flags="hfq_factor_current"
    if isinstance(objs,str):
        if objs=="all":
            for obj in rr.keys():
                r = rr[obj]
                if startdate:
                    if enddate:
                        r = r.loc[(r.index >= startdate) & (r.index <= enddate)]
                    else:
                        r = r.loc[r.index >= startdate]
                else:
                    if enddate:
                        r = r.loc[r.index <= enddate]
                if len(r) >= 2:
                    r = r.copy(deep=True)
                    if flag != "qfq":  # 前复权，date倒叙
                        r = r.sort_index()
                        r.loc[r.index[0], flags] = 1
                        r["factor"] = np.cumprod(r[flags])
                        n = len(r.index)
                        for i in range(n):
                            if i > 0 and i < n - 1:
                                t1 = r.index[i]
                                t2 = r.index[i + 1]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1) & (k["timekey"] < t2)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                            elif i == n - 1:
                                t1 = r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                    else:
                        r.loc[r.index[0], flags] = 1
                        r["factor"] = np.cumprod(r[flags])
                        n = len(r.index)
                        for i in range(n):
                            if i > 0 and i < n - 1:
                                t1 = r.index[i]
                                t2 = r.index[i + 1]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1) & (k["timekey"] > t2)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                            elif i == n - 1:
                                t1 = r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
        else:
            obj=objs
            if obj in rr.keys():
                r = rr[obj]
                if startdate:
                    if enddate:
                        r = r.loc[(r.index >= startdate) & (r.index <= enddate)]
                    else:
                        r = r.loc[r.index >= startdate]
                else:
                    if enddate:
                        r = r.loc[r.index <= enddate]
                if len(r)>=2:
                    r = r.copy(deep=True)
                    if flag != "qfq":  # 前复权，date倒叙
                        r = r.sort_index()
                        r.loc[r.index[0], flags] = 1
                        r["factor"] = np.cumprod(r[flags])
                        n = len(r.index)
                        for i in range(n):
                            if i > 0 and i < n - 1:
                                t1 = r.index[i]
                                t2 = r.index[i + 1]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1) & (k["timekey"] < t2)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                            elif i == n - 1:
                                t1 = r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                    else:
                        r.loc[r.index[0], flags] = 1
                        r["factor"] = np.cumprod(r[flags])
                        n = len(r.index)
                        for i in range(n):
                            if i > 0 and i < n - 1:
                                t1 = r.index[i]
                                t2 = r.index[i + 1]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1) & (k["timekey"] > t2)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                            elif i == n - 1:
                                t1 = r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
    else:
        for obj in objs:
            if obj in rr.keys():
                r=rr[obj]
                if startdate:
                    if enddate:
                        r = r.loc[(r.index >= startdate) & (r.index <= enddate)]
                    else:
                        r = r.loc[r.index >= startdate]
                else:
                    if enddate:
                        r = r.loc[r.index <= enddate]
                if len(r)>=2:
                    r=r.copy(deep=True)
                    if flag != "qfq":#前复权，date倒叙
                        r=r.sort_index()
                        r.loc[r.index[0], flags] = 1
                        r["factor"]=np.cumprod(r[flags])
                        n=len(r.index)
                        for i in range(n):
                            if i>0 and i<n-1:
                                t1=r.index[i]
                                t2=r.index[i+1]
                                rdata=r.loc[t1,"factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1) & (k["timekey"] < t2)
                                k.loc[tt, feild_r] = k.loc[tt,feild_r] * rdata
                            elif i==n-1:
                                t1=r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] >= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                    else:
                        r.loc[r.index[0], flags] = 1
                        r["factor"] = np.cumprod(r[flags])
                        n = len(r.index)
                        for i in range(n):
                            if i > 0 and i < n - 1:
                                t1 = r.index[i]
                                t2 = r.index[i + 1]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1) & (k["timekey"] > t2)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
                            elif i == n - 1:
                                t1 = r.index[i]
                                rdata = r.loc[t1, "factor"]
                                tt = (k["obj"] == obj) & (k["timekey"] <= t1)
                                k.loc[tt, feild_r] = k.loc[tt, feild_r] * rdata
    k=df_round(k,feild_r,2)
    return k
def getstock_kdata0(objs,ktype="k1d",startdate=None,enddate=None,adjustflag="hfq",feild=None,dtype="df"):
    global url_stock_bydate, url_stock_byobj, url_index_byobj, url_right, rr
    """

    Args:
        objs: str,一个代码或是  all or list:[] 交易所代码
        ktype: str :k1d,k1dw,k1M,K5m,k15m,k30m,k60m
        startdate:str :YYYYMMDD,None:全部
        enddate:str :YYYYMMDD,None:全部
        adjustflag:str :qfq,hfq,bfq
        feild:list or none
        dtype:str:df or other

    Returns:
        objs str:单重索引的数据框
        objs list :多重索引数据框
    """
    rst=pd.DataFrame()
    if isinstance(objs,str):
        if objs=="all":
            kl=os.listdir(os.path.abspath(os.path.join(url_stock_bydate,ktype)))
            klr = []
            k = []
            if startdate:
                if enddate:
                    starts=datetime.datetime.strptime(startdate,"%Y%m%d")
                    starts=starts.strftime(freq[ktype])
                    ends = datetime.datetime.strptime(enddate, "%Y%m%d")
                    ends = ends.strftime(freq[ktype])
                    for i in kl:
                        freqs=i.split("_")[0]
                        if freqs>=starts and freqs<=ends:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_bydate, ktype, i))
                        k1 = load(url)
                        if k1:
                            for j in k1.values():
                                k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst = pd.DataFrame(k)
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate), feild]
                        else:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate)]
                else:
                    starts = datetime.datetime.strptime(startdate, "%Y%m%d")
                    starts = starts.strftime(freq[ktype])
                    for i in kl:
                        freqs = i.split("_")[0]
                        if freqs >= starts:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_bydate, ktype, i))
                        k1 = load(url)
                        if k1:
                            for j in k1.values():
                                k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst = pd.DataFrame(k)
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[k1 >= startdate, feild]
                        else:
                            rst = rst.loc[k1 >= startdate]
            else:
                if enddate:
                    ends = datetime.datetime.strptime(url_stock_bydate, "%Y%m%d")
                    ends = ends.strftime(freq[ktype])
                    for i in kl:
                        freqs = i.split("_")[0]
                        if freqs <= ends:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i))
                        k1 = load(url)
                        if k1:
                            for j in k1.values():
                                k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst = pd.DataFrame(k)
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[k1 <= enddate, feild]
                        else:
                            rst = rst.loc[k1 <= enddate]
                else:
                    klr=kl
                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_bydate, ktype, i))
                        k1 = load(url)
                        if k1:
                            for j in k1.values():
                                k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst = pd.DataFrame(k)
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        if feild:
                            holdfield(rst,feild)
        else:
            url=os.path.abspath(os.path.join(url_stock_byobj,ktype,objs+"_"+ktype+".pkl"))
            k=load(url)
            if k:
                rst = pd.DataFrame(k)
                rst.index=rst["timekey"]
                rst.sort_index(inplace=True)
                if startdate:
                    if enddate:
                        if feild:
                            rst = rst.loc[(rst.index >= startdate) & (rst.index <= enddate), feild]
                        else:
                            rst = rst.loc[(rst.index >= startdate) & (rst.index <= enddate)]
                    else:
                        if feild:
                            rst = rst.loc[rst.index >= startdate, feild]
                        else:
                            rst = rst.loc[rst.index >= startdate]
                else:
                    if enddate:
                        if feild:
                            rst = rst.loc[rst.index <= enddate, feild]
                        else:
                            rst = rst.loc[rst.index <= enddate]
                    else:
                        if feild:
                            holdfield(rst,feild)
            else:
                print("no exist",objs,ktype)
    elif isinstance(objs,list):
        score_objs=len(objs)/objs_len
        if startdate:
            starts = datetime.datetime.strptime(startdate, "%Y%m%d")
        else:
            starts=startdate0
        if enddate:
            ends = datetime.datetime.strptime(enddate, "%Y%m%d")
        else:
            ends=enddate0
        score_date=(ends-starts).days/alldays
        if 2*score_date>score_objs:#按obj 读取
            k = []
            for i in objs:
                url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i + "_" + ktype + ".pkl"))
                k1 = load(url)
                if k1:
                    k.extend(k1)
                else:
                    print("no exist", i, ktype)
            if k:
                rst = pd.DataFrame(k)
                rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                rst.sort_index(inplace=True)
                k1 = rst.index.get_level_values("timekey")
                if startdate:
                    if enddate:
                        if feild:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate), feild]
                        else:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate)]
                    else:
                        if feild:
                            rst = rst.loc[k1 >= startdate, feild]
                        else:
                            rst = rst.loc[k1 >= startdate]
                else:
                    if enddate:
                        if feild:
                            rst = rst.loc[k1 <= enddate, feild]
                        else:
                            rst = rst.loc[k1 <= enddate]
                    else:
                        if feild:
                            holdfield(rst,feild)
        else:#按 date读取
            kl = os.listdir(os.path.abspath(os.path.join(url_stock_bydate, ktype)))
            klr = []
            k = []
            if startdate:
                if enddate:
                    starts = datetime.datetime.strptime(startdate, "%Y%m%d")
                    starts = starts.strftime(freq[ktype])
                    ends = datetime.datetime.strptime(enddate, "%Y%m%d")
                    ends = ends.strftime(freq[ktype])
                    for i in kl:
                        freqs = i.split("_")[0]
                        if freqs >= starts and freqs <= ends:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i))
                        k1 = load(url)
                        if k1:
                            for k,j in k1.items():
                                if k in objs:
                                    k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate), feild]
                        else:
                            rst = rst.loc[(k1 >= startdate) & (k1 <= enddate)]
                else:
                    starts = datetime.datetime.strptime(startdate, freq[ktype])
                    starts = starts.strftime(freq[ktype])
                    for i in kl:
                        freqs = i.split("_")[0]
                        if freqs >= starts:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i))
                        k1 = load(url)
                        if k1:
                            for k, j in k1.items():
                                if k in objs:
                                    k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[k1 >= startdate, feild]
                        else:
                            rst = rst.loc[k1 >= startdate]
            else:
                if enddate:
                    ends = datetime.datetime.strptime(enddate, "%Y%m%d")
                    ends = ends.strftime(freq[ktype])
                    for i in kl:
                        freqs = i.split("_")[0]
                        if freqs <= ends:
                            klr.append(i)

                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i))
                        k1 = load(url)
                        if k1:
                            for k, j in k1.items():
                                if k in objs:
                                    k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        k1 = rst.index.get_level_values("timekey")
                        if feild:
                            rst = rst.loc[k1 <= enddate, feild]
                        else:
                            rst = rst.loc[k1 <= enddate]
                else:
                    klr = kl
                    for i in klr:
                        url = os.path.abspath(os.path.join(url_stock_byobj, ktype, i))
                        k1 = load(url)
                        if k1:
                            for k, j in k1.items():
                                if k in objs:
                                    k.extend(j)
                        else:
                            print("no exist", i, ktype)
                    if k:
                        rst.set_index(["timekey", "obj"], inplace=True,drop=False)
                        rst.sort_index(inplace=True)
                        if feild:
                            holdfield(rst, feild)
    if adjustflag!="bfq":
        rst=adjust(objs,rst,adjustflag,startdate,enddate)
    if dtype != "df":
        rst = rst.to_dict("index")
    return rst

def getindex_kdata(objs,ktype="k1d",startdate=None,enddate=None,feild=None,dtype="df"):
    """

    Args:
        objs: str,一个代码或是  or list:[] 交易所代码
        ktype: str :k1d,k1dw,k1M
        startdate:str :YYYYMMDD,None:全部
        enddate:str :YYYYMMDD,None:全部
        feild:list or none
        dtype:str:df or other

    Returns:
        objs str:单重索引的数据框
        objs list :多重索引数据框
    """
    global url_stock_bydate, url_stock_byobj, url_index_byobj, url_right, rr
    rst=pd.DataFrame()
    if isinstance(objs,str):
        url=os.path.abspath(os.path.join(url_index_byobj,ktype,objs+"_"+ktype+".pkl"))
        k=load(url)
        if k:
            rst = pd.DataFrame(k)
            rst.index=rst["timekey"]
            rst.sort_index(inplace=True)
            if startdate:
                if enddate:
                    if feild:
                        rst = rst.loc[(rst.index >= startdate) & (rst.index <= enddate), feild]
                    else:
                        rst = rst.loc[(rst.index >= startdate) & (rst.index <= enddate)]
                else:
                    if feild:
                        rst = rst.loc[rst.index >= startdate, feild]
                    else:
                        rst = rst.loc[rst.index >= startdate]
            else:
                if enddate:
                    if feild:
                        rst = rst.loc[rst.index <= enddate, feild]
                    else:
                        rst = rst.loc[rst.index <= enddate]
                else:
                    if feild:
                        holdfield(rst,feild)
        else:
            print("no exist",objs,ktype)
    elif isinstance(objs,list):
        k = []
        for i in objs:
            url = os.path.abspath(os.path.join(url_index_byobj, ktype, i + "_" + ktype + ".pkl"))
            k1 = load(url)
            if k1:
                k.extend(k1)
            else:
                print("no exist", i, ktype)
        if k:
            rst = pd.DataFrame(k)
            rst.set_index(["timekey", "obj"], inplace=True,drop=False)
            rst.sort_index(inplace=True)
            k1 = rst.index.get_level_values("timekey")
            if startdate:
                if enddate:
                    if feild:
                        rst = rst.loc[(k1 >= startdate) & (k1 <= enddate), feild]
                    else:
                        rst = rst.loc[(k1 >= startdate) & (k1 <= enddate)]
                else:
                    if feild:
                        rst = rst.loc[k1 >= startdate, feild]
                    else:
                        rst = rst.loc[k1 >= startdate]
            else:
                if enddate:
                    if feild:
                        rst = rst.loc[k1 <= enddate, feild]
                    else:
                        rst = rst.loc[k1 <= enddate]
                else:
                    if feild:
                        holdfield(rst,feild)
    if dtype != "df":
        rst = rst.to_dict("index")
    return rst